/**
 * Created by oneminute on 7/28/14.
 */

var assert = require("assert");
var should = require("should");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();
var httpMocks = require("../lib/http-mock");
var async = require("async");
var path = require("path");
var updateuserdetails = require("../../routes/updateuserdetails");
var userFilter = require("../../routes/userfilter");

describe("测试routes/updateuserdetails模块。", function () {

    var user = null;
    before(function (done) {
        user = new lz.User({
            id: lz.Utils.getUUID("u_"),
            name: lz.Utils.getRandName()
        });
        user.save(function (_err, _user) {
            done();
        })
    });

    it("正常更新用户信息。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/updateuserdetails?uid=' + user.id,
            body: {
                lat: 100,
                lon: 50,
                inbusiness: true,
                phone: "18652882211",
                location: "测试地址",
                description: "测试描述"
            }
        });
        var response = httpMocks.createExpressResponse();

        var getFn = updateuserdetails.post.bind(null, request, response, {}, function (_err, _user, _args) {
            testutils.check(done, function () {
                assert.notEqual(_user, null);
                assert.equal(_user.lat, 100);
                assert.equal(_user.lon, 50);
                assert.equal(_user.inbusiness, true);
                _user.phone.should.equal("18652882211");
                _user.address.should.equal("测试地址");
                _user.description.should.equal("测试描述");
            });
        });
        userFilter(request, response, getFn);
    });

    it("使用不存在的uid更新客户信息。", function (done) {
        var request = httpMocks.createExpressRequest({
            method: 'GET',
            url: '/updateuserdetails?uid=' + lz.Utils.getUUID("u_"),
            body: {
                lat: 100,
                lon: 50,
                inbusiness: true,
                phone: "18652882211",
                location: "测试地址",
                description: "测试描述"
            }
        });
        var response = httpMocks.createExpressResponse();

        var getFn = updateuserdetails.post.bind(null, request, response, {}, function (_err, _user, _args) {
            testutils.check(done, function () {
                assert.equal(_err.code, lz.Config.errors.REQUEST_TEMPUSER_ERROR.code);
            });
        });
        userFilter(request, response, getFn);
    });

    after(function (done) {
        user.delete(function () {
            done();
        });
    });
});